<?php

namespace tools;
class MenuTools
{
    /**
     * 获取菜单并格式化成Tree模块
     * @param null $role_id 角色ID，不传则默认不选择
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getMenu($role_id = null)
    {
        if ($role_id) {
            $role = \app\admin\model\RoleModel::with('permissions')->where('id', $role_id)->find();

            $menus = $role->permissions->map(function ($item) {
                return $item->controller . '/' . $item->method;
            })->toArray();
        } else {
            $menus = [];
        }

        $data = [];
        $menu = config('menus.');

        $i = 0;
        foreach ($menu as $k1 => $v1) {
            //获取第一层
            $data[$i]['title'] = $v1['name'];
            $data[$i]['id'] = $k1;
            $j = 0;
            foreach ($v1['nodes'] as $k2 => $v2) {
                //获取第二层
                $data[$i]['children'][$j]['title'] = $v2['name'];
                $data[$i]['children'][$j]['id'] = $k2;
                $k = 0;
                foreach ($v2['method'] as $k3 => $v3) {
                    //获取第三层
                    $data[$i]['children'][$j]['children'][$k]['title'] = $v3['name'];
                    $data[$i]['children'][$j]['children'][$k]['id'] = $k2 . '/' . $v3['function'];
                    $data[$i]['children'][$j]['children'][$k]['checked'] = in_array($k2 . '/' . $v3['function'], $menus);
                    $k++;
                }
                $j++;
            }
            $i++;
        }

        return $data;
    }

    /**
     * 将Tree解压
     * @param array $menu Tree标准的数据
     * @return array
     */
    public static function deMenu($menu)
    {
        static $data = [];
        foreach ($menu as $k => $item) {
            if (isset($item['children'])) {
                self::deMenu($item['children']);
            } else {
                $data[] = $item['id'];
            }
        }

        return $data;
    }

    /**
     * 赋予角色权限
     * @param int $role_id 角色ID
     * @param array $diy_menu 要设置的并且封装号的菜单列表
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public static function setMenu($role_id, $diy_menu)
    {
        \app\admin\model\PermissionModel::where('role_id', $role_id)->delete();
        $menu = config('menus.');
        foreach ($menu as $k1 => $v1) {
            foreach ($v1['nodes'] as $k2 => $v2) {
                foreach ($v2['method'] as $k3 => $v3) {
                    $data['controller'] = $k2;
                    $data['method'] = $v3['function'];
                    $data['role_id'] = $role_id;
                    $data['level1'] = $v1['name'];
                    $data['level2'] = $v2['name'];
                    $data['level3'] = $v3['name'];
                    if (in_array($data['controller'] . '/' . $data['method'], $diy_menu)) {
                        \app\admin\model\PermissionModel::create($data);
                    }
                }
            }
        }
    }

}
